package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    /**
     * 套餐分页查询
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> list = setMealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(list.getTotal(), list.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 添加套餐
        setMealMapper.addSetMeal(setmeal);
        // 添加套餐包含的菜品
        Long id = setmeal.getId();
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        if(dishes != null && dishes.size() > 0) {
            dishes.forEach(dish -> {
                dish.setSetmealId(id);
            });
            // 批量插入菜品
            setMealDishMapper.addDishBatch(dishes);
        }
    }

    /**
     * 根据 ID 查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectSetMealDataById(Long id) {
        // 先查询套餐
        SetmealVO setmealVO = setMealMapper.selectSetMealDataById(id);
        // 再查询套餐包含菜品
        List<SetmealDish> setmealDishes = setMealDishMapper.selectDishBySetMeal(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 批量删除套餐
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSetMealBatch(List<Integer> ids) {
        // 起售中的套餐不能删除
        for(Integer id : ids) {
            Integer state = setMealMapper.checkStatus(id);
            if (Objects.equals(state, StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 删除套餐
        setMealMapper.deleteSetMealBatch(ids);
        // 删除套餐关联的菜品
        setMealDishMapper.deleteDishBySetMeals(ids);
    }

    /**
     * 套餐起售、停售
     * @param id
     * @param status
     */
    @Override
    public void setSetMealStatus(Integer id, Integer status) {
        // 套餐内包含未启售菜品，无法启售
        //setMealMapper.selectDishStatus(id);
        Integer num = setMealDishMapper.countNotOnSaleNum(id);
        if(num != null && num > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        setMealMapper.setSetMealStatus(id, status);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 修改套餐的基本属性
        setMealMapper.updateSetMeal(setmeal);
        // ID 回填
        Long id = setmeal.getId();
        // 删除套餐关联菜品（防止残留，即使没有也要删除）
        setMealDishMapper.deleteDishBySetMeal(id);
        // 重新插入菜品
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        if(dishes != null && dishes.size() > 0) {
            dishes.forEach(dish -> {
                dish.setSetmealId(id);
            });
            // 批量插入菜品
            setMealDishMapper.addDishBatch(dishes);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
